﻿@model Order

@functions {
    private string GetCategoryNameId(Category category)
    {
        while (category.Level != 2)
        {
            category = category.Parent;
        }

        return category.NameId;
    }
}

@{
    var strHp = new StringHelper();
    var imgHp = new ImageHelper();
    var dateHp = new DateHelper();

    ApplicationUser sender = ViewBag.Sender;
    float exchangeRate = ViewBag.ExchangeRate;

    string domain = "http://chomy.com.vn";
    List<OrderStyle> orderStyles = Model.OrderStyles;
    float totalPrice = 0;
    float totalQuantity = 0;

    string td = "border: 1px solid #ddd; padding: 8px; vertical-align: middle;";
    string table = "width: 100%; border: 1px solid #ddd";
    string bgTitle = "background-color: #333; color: #fff; padding: 8px 15px; border-radius: 4px; border-left: 10px solid #777; margin-bottom: 15px;";
}

<div style="background-color: #777; padding:15px 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif">
    <div style="background-color: #fff; width: 600px; margin: auto; padding: 15px;">
        <div>
            <div style="width: 300px; float: left;">
                <a href="@domain" target="_blank">
                    <img src="@domain/img/logo.png" height="80" />
                </a>
            </div>
            <div style="width: 300px; float:left; text-align: right;">
                <div style="font-style: italic; color: #aaa; margin: 15px 0 10px 0;">
                    @Model.DateCreated.ToString("dd/MM/yyyy - hh:mm tt")
                </div>
                <div>
                    <span style="background-color: #5CB85C; color: #fff; padding: .2em .6em .3em; font-weight: bold; border-radius: .25em; margin-right: 5px;">
                        Đã thanh toán
                    </span>
                    <span style="background-color: #333; color: #fff; padding: .2em .6em .3em; font-weight: bold; border-radius: .25em;">
                        @Model.Code
                    </span>
                </div>

            </div>
        </div>
        <div style="clear: both;"></div>
        <br />

        <!-- Sender -->
        <div style="@bgTitle">
            Thông tin người đặt hàng
        </div>
        <table style="@table" cellpadding="0" cellspacing="0">
            <tr>
                <td style="@td" width="70">Họ tên</td>
                <td style="@td">
                    <b>@sender.Name</b>
                </td>
            </tr>
            <tr>
                <td style="@td">Địa chỉ</td>
                <td style="@td">
                    <b>@sender.Address</b>
                </td>
            </tr>
            <tr>
                <td style="@td">Điện thoại</td>
                <td style="@td">
                    <b>@sender.PhoneNumber</b>
                </td>
            </tr>
            <tr>
                <td style="@td">Email</td>
                <td style="@td">
                    <b>@sender.Email</b>
                </td>
            </tr>
        </table>
        <br />

        <!-- Receiver -->
        <div style="@bgTitle">
            Thông tin người nhận hàng
        </div>
        <table style="@table" cellpadding="0" cellspacing="0">
            <tr>
                <td style="@td" width="70">Họ tên</td>
                <td style="@td">
                    <b>@Model.Name</b>
                </td>
            </tr>
            <tr>
                <td style="@td">Địa chỉ</td>
                <td style="@td">
                    <b>@Model.Address</b>
                </td>
            </tr>
            <tr>
                <td style="@td">Điện thoại</td>
                <td style="@td">
                    <b>@Model.Phone</b>
                </td>
            </tr>
            <tr>
                <td style="@td">Email</td>
                <td style="@td">
                    <b>@Model.Email</b>
                </td>
            </tr>
        </table>
        <br />

        <!-- Details -->
        <div style="@bgTitle">
            Thông tin đơn hàng
        </div>
        <table style="@table" cellpadding="0" cellspacing="0">
            <tr>
                <td colspan="2" style="@td">
                    Sản phẩm
                </td>
                <td style="@td text-align: right; ">
                    Thành tiền
                </td>
            </tr>
            @foreach (OrderStyle item in orderStyles)
            {
                // Model
                Style style = item.Style;
                Product product = style.Product;

                // Price
                float quantityPricing = item.Quantity * style.UnitPrice;
                totalPrice += quantityPricing;
                totalQuantity += item.Quantity;

                // Image
                Property color = style.StyleProperties
                    .Select(x => x.Property)
                    .FirstOrDefault(x => x.PropertyType.NameId == "color");
                Image image = new Image();
                if (color != null)
                {
                    image = product.Images
                        .Where(x => x.PropertyId == color.Id)
                        .OrderByDescending(x => x.Priority)
                        .FirstOrDefault();
                }
                else
                {
                    image = product.Images
                        .OrderByDescending(x => x.Priority)
                        .FirstOrDefault();
                }

                string url = Url.Action("Details", "Product", new
                {
                    productNameId = product.NameId,
                    categoryNameId = GetCategoryNameId(product.Category)
                });

                <tr>
                    <td style="@td" width="60">
                        <img src="@(domain + image.Url)" width="60" />
                    </td>
                    <td style="@td">
                        <div style="margin-bottom: 8px;">
                            <b><a href="@(domain + url)" target="_blank">@product.Name</a></b>
                        </div>
                        <div style="margin-bottom: 8px; color: #777;">@product.Code</div>
                        <div style="margin-bottom: 8px;">
                            @foreach (Property p in style.StyleProperties.Select(x => x.Property).OrderBy(x => x.Name))
                            {
                                <span style="margin-right: 10px;">
                                    <span style="color: #777;">@p.PropertyType.Name:</span> <b>@p.Name</b>
                                </span>
                            }
                        </div>
                        <div>
                            <span style="margin-right: 10px;">
                                <span style="color: #777;">Đơn giá:</span> <b>@strHp.PriceFormat(style.UnitPrice * exchangeRate, "vnd")</b>
                            </span>
                            <span style="margin-right: 10px;">
                                <span style="color: #777;">SL:</span> <b>@item.Quantity</b>
                            </span>
                        </div>
                    </td>
                    <td style="@td text-align: right;" width="110">
                        <b>@strHp.PriceFormat(quantityPricing * exchangeRate, "vnd")</b>
                    </td>
                </tr>
            }
            <tr>
                <td colspan="2" style="@td text-align: right">
                    Tổng cộng
                </td>
                <td style="@td text-align: right">
                    <b>@strHp.PriceFormat(totalPrice * exchangeRate, "vnd")</b>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="@td text-align: right">
                    VAT (10%) @{ float vat = totalPrice * 10 / 100; }
                </td>
                <td style="@td text-align: right;">
                    <b style="color: #A94442;">+ @strHp.PriceFormat(vat * exchangeRate, "vnd")</b>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="@td text-align: right; background-color: #777; color: #fff;">
                    Tổng thanh toán @{ float checkoutPrice = totalPrice + vat; }
                </td>
                <td style="@td text-align: right; background-color: #777; color: #fff;">
                    <b>@strHp.PriceFormat(checkoutPrice * exchangeRate, "vnd")</b>
                </td>
            </tr>
        </table>
        <br />
        <strong style="color: #A94442;">Thời gian giao hàng dự kiện từ @dateHp.BusinessDaysTo(3).ToString("d/M") đến @dateHp.BusinessDaysTo(5).ToString("d/M") (có thể sớm hơn)</strong>
        <br />
        <p>
            <span style="color: #777;">Liên hệ tư vấn: </span> <b>086.684.5561</b>
        </p>
        <p>
            <span style="color: #777;">Hotline: </span> <b>096.215.8661 (24/7)</b>
        </p>
    </div>
</div>